using System;
using System.Linq;
using MallSystembackend.Api.Model;
using MallSystembackend.Api.ParamModel;
using MallSystembackend.Api.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CommodityTypeController : ControllerBase //商品类型路由
    {
        public IRepository<CommodityType> _commodityTypeRepository;

        public readonly IConfiguration _configuration;

        public CommodityTypeController(
            IRepository<CommodityType> commodityTypeRepository,
            IConfiguration configuration
        )
        {
            _commodityTypeRepository = commodityTypeRepository;
            _configuration = configuration;
        }

        //根据品类名
        [HttpGet, Route("select")]
        public ActionResult SelectTypename([FromQuery] ParamSelect data)
        {
            var msg = data.msg;

            //获取当前页
            var currentPage = data.currentPage;

            //获取页面数据行数
            var pageSize = data.pageSize;

            var types = _commodityTypeRepository.Table;
            var type =
                types
                    .Where(x => x.TypeName.Contains(msg))
                    .Where(x => x.IsDeleted == false)
                    .Skip((currentPage - 1) * pageSize)
                    .Take(pageSize)
                    .ToList();

            return Ok(new {
                Code = 1000,
                Data = type,
                Count =
                    types
                        .Where(x => x.TypeName.Contains(msg))
                        .Where(x => x.IsDeleted == false)
                        .Count()
            });
        }

        //添加分类
        [HttpPost]
        public ActionResult AddComType(ParamCommodityType model)
        {
            var addComType = new CommodityType { TypeName = model.TypeName, IsActived = true };
            _commodityTypeRepository.Insert (addComType);
            return Ok(new { Code = 1000, Data = addComType, Msg = "获得成功" });
        }

        //编辑分类
        [HttpPut("{id}")]
        public ActionResult UpdateType(int id, ParamCommodityType model)
        {
            var Type = _commodityTypeRepository.GetById(id);
            if (Type != null)
            {
                Type.TypeName = model.TypeName;
                _commodityTypeRepository.Update (Type);
                return Ok(new { Code = 1000, Data = Type, Msg = "修改成功" });
            }
            return Ok(new { Code = 1002, Data = " ", Msg = "抱歉，未找到" });
        }

        //获得所有分类
        [HttpGet]
        public ActionResult GetAny([FromQuery] ParamPager model)
        {
            var CurrentPage = model.currentPage;

            var PageSize = model.pageSize;

            var TypeAnys = _commodityTypeRepository.Table;
            var TypeAny =
                TypeAnys
                    .Where(x => x.IsDeleted == false)
                    .Skip((CurrentPage - 1) * PageSize)
                    .Take(PageSize)
                    .ToList();
            return Ok(new {
                Code = 1000,
                Data = TypeAny,
                Count = TypeAnys.Where(x => x.IsDeleted == false).Count(),
                Msg = "获得成功"
            });
        }

        //修改分类状态
        [HttpPut, Route("statu/{id}")]
        public ActionResult PutStatu(ParamStatus model, int id)
        {
            var type = _commodityTypeRepository.GetById(id);
            if (type != null)
            {
                if (model.isActived == false)
                {
                    type.IsActived = false;
                    _commodityTypeRepository.Update (type);
                    return Ok(new {
                        Code = 1000,
                        Data = type,
                        Msg = "修改成功"
                    });
                }
                else
                {
                    type.IsActived = true;
                    _commodityTypeRepository.Update (type);
                    return Ok(new {
                        Code = 1000,
                        Data = type,
                        Msg = "修改成功"
                    });
                }
            }
            return Ok(new {
                Code = 1002,
                Data = "",
                Msg = "获取失败，请检查id是否正确"
            });
        }

        //删除分类
        [HttpDelete("{id}")]
        public ActionResult DeleteType(int id)
        {
            var type = _commodityTypeRepository.GetById(id);
            if (type != null)
            {
                type.IsActived = false;
                type.IsDeleted = true;
                type.UpdatedTime = DateTime.Now;

                _commodityTypeRepository.Delete (type);

                return Ok(new {
                    Code = 1000,
                    Data = type,
                    Msg = string.Format("删除指定id为{0}的类型成功", id)
                });
            }
            else
            {
                return Ok(new {
                    Code = 1002,
                    Data = "",
                    Msg = "指定Id的类型不存在，请确认后重试"
                });
            }
        }
    }
}
